/**
 * testrunner.js
 * 
 * Wrapper object for unit tests.
 * 
 * @author A.Weissflog
 * 
 * (C) 2010 A.Weissflog
 */

/**
 * Default constructor. Create a TestRunner object and add
 * test functions to it, then call TestRunner.Run().
 * 
 * @param {String} name
 */
function TestRunner(sessionName)
{
	this.sessionName = sessionName;
	this.tests = new Array();
}

/**
 * Add a test function to the test runner. The function
 * must return a bool when called.
 * 
 * @param {String} name
 * @param {Function} testFunc
 */
TestRunner.prototype.Add = function(name, testFunc)
{
	this.tests.push({name: name, func: testFunc});
}

/**
 * Run the added test functions and print result to web page.
 */
TestRunner.prototype.Run = function()
{
	document.write("Running tests: " + this.sessionName + "<br><br>");
	var numOk = 0;
	var numFailed = 0;
	var i;
	for (i = 0; i < this.tests.length; i++) {
		if (this.tests[i].func()) {
			document.write(this.tests[i].name + ": ok <br>");
			numOk++;
		}
		else {
			document.write(this.tests[i].name + ": FAILED <br>");
			numFailed++;
		}
	}
	if (numFailed == 0) {
		document.write("<br>All tests ok!<br>");
	}
	else {
		document.write("<br>" + numOk + " tests ok, " + numFailed + " tests FAILED!<br>");
	}
}
